package com.hustbaidu.hustmap;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.View;

public class PathSegmentView extends View {
	private int startX, startY;
	private int stopX, stopY;
	private int width, height;
	private int offsetX, offsetY;
	private int relativeStartX, relativeStartY;
	private int relativeStopX, relativeStopY;

	public PathSegmentView(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		// TODO Auto-generated constructor stub
	}

	public PathSegmentView(Context context, AttributeSet attrs) {
		super(context, attrs);
		// TODO Auto-generated constructor stub
	}

	public PathSegmentView(Context context) {
		super(context);
		// TODO Auto-generated constructor stub
		setClickable(false);
	}

	@Override
	protected void onDraw(Canvas canvas) {
		// TODO Auto-generated method stub
		super.onDraw(canvas);
		
		canvas.drawColor(Color.TRANSPARENT);
		Paint paint = new Paint();
		paint.setColor(0xa00000ff);
		paint.setStrokeWidth(5);
		paint.setAntiAlias(true);
		
		
//		System.out.println(relativeStartX + " : " + relativeStartY + " : " + relativeStopX + " : " + relativeStopY);
		canvas.drawLine(relativeStartX, relativeStartY, relativeStopX, relativeStopY, paint);
	}
	
	@Override
	protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
		// TODO Auto-generated method stub
//		super.onMeasure(widthMeasureSpec, heightMeasureSpec);
		setMeasuredDimension(width, height);
	}
	
	public void move(int distanceX, int distanceY){
		offsetX = offsetX - distanceX;
		offsetY = offsetY - distanceY;
	}

	public void setPositionInfo(int startX, int startY, int stopX, int stopY, 
			int currentChipX, int currentChipY, int currentOffsetX, int currentOffsetY, 
			int mapChipWidth, int mapChipHeight){
		this.offsetX = (startX < stopX ? startX : stopX) - currentChipX * mapChipWidth - currentOffsetX;
		this.offsetY = (startY < stopY ? startY : stopY) - currentChipY * mapChipHeight - currentOffsetY;
		this.startX = startX;
		this.startY = startY;
		this.stopX = stopX;
		this.stopY = stopY;
		updateSizeAndSetPoint();
	}
	
	private void updateSizeAndSetPoint(){
		
		/*operations for X*/
		int distanceX = Math.abs(startX - stopX);
		if(distanceX < 10){
			width = 10;
		}
		else{
			width = distanceX;
		}
		
		if(startX < stopX){
			relativeStartX = (width - distanceX) / 2;
			relativeStopX = (width + distanceX) / 2;
			offsetX -= relativeStartX;
		}
		else{
			relativeStartX = (width + distanceX) / 2;
			relativeStopX = (width - distanceX) / 2;
			offsetX -= relativeStopX;
		}
		
		
		/*operations for Y*/
		int distanceY = Math.abs(startY - stopY);
		if(distanceY < 10){
			height = 10;
		}
		else{
			height = distanceY;
		}
		
		if(startY < stopY){
			relativeStartY = (height - distanceY) / 2;
			relativeStopY = (height + distanceY) / 2;
			offsetY -= relativeStartY;
		}
		else{
			relativeStartY = (height + distanceY) / 2;
			relativeStopY = (height - distanceY) / 2;
			offsetY -= relativeStopY;
		}
		
//		System.out.println(offsetX + " : " + offsetY + " : " + width + " : " + height);
	}

	public int getOffsetX() {
		return offsetX;
	}

	public int getOffsetY() {
		return offsetY;
	}

	public int getRelativeStartX() {
		return relativeStartX;
	}

	public int getRelativeStartY() {
		return relativeStartY;
	}

	public int getRelativeStopX() {
		return relativeStopX;
	}

	public int getRelativeStopY() {
		return relativeStopY;
	}
	
	
	
}
